//
//  LabelTextfieldView.m
//  HuaRun2
//
//  Created by admin on 17/8/30.
//  Copyright © 2017年 Sunny. All rights reserved.
//

#import "LabelTextfieldView.h"
#import "LineView.h"


@interface LabelTextfieldView ()

//线1
@property (nonatomic, strong) LineView * line1;
@end

@implementation LabelTextfieldView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
#pragma mark - life cycle
- (void)dealloc {
    NSLog(@"%@ dealloc", [self class]);
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self viewInit];
        [self layoutInit];
    }
    return self;
}

- (void)viewInit {
    [self addSubview:self.starLab];
    
    [self addSubview:self.nameLab];
    [self addSubview:self.textFieldBgView];
    [self addSubview:self.textField];
    [self addSubview:self.infoLab];
    [self addSubview:self.line1];

//    self.nameLab.backgroundColor = kRandomColor;
//    self.textField.backgroundColor = [UIColor redColor];
//    self.backgroundColor = kRandomColor;
}

- (void)layoutInit {
    weakself(ws);
    
    [self.starLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(ws.nameLab.mas_left).offset(2);
        make.height.centerY.mas_equalTo(ws.nameLab);
    }];
    
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.centerY.mas_equalTo(ws.textField);
        make.right.mas_equalTo(ws.textField.mas_left).offset(-kMargin15);
//        make.width.mas_equalTo(100);
    }];
    
    [self.textFieldBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.mas_equalTo(ws.textField);
        make.left.mas_equalTo(ws.textField).offset(-5);
    }];
    
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(ws).offset(-kMargin15);
        make.centerY.mas_equalTo(ws);
        make.height.mas_equalTo(35);
        make.width.mas_equalTo(ws).multipliedBy(0.6);
    }];

    
    [self.infoLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(ws.textField);
        make.height.mas_equalTo(13);
        make.top.mas_equalTo(ws.textField.mas_bottom).offset(2);
    }];
    
    [self.line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws).offset(kMargin10);
        make.right.mas_equalTo(ws).offset(-kMargin10);
        make.height.mas_equalTo(1);
        make.bottom.mas_equalTo(ws);
    }];
    
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(ws);
        make.size.mas_equalTo(CGSizeMake(kMainScreenWidth, 35));
    }];
    
}
#pragma mark - method 
- (BOOL) canNextStepTextFieldDidChange {
    
    NSString * text = _textField.text;
    if (!self.starLab.hidden) {
    
        _infoLab.hidden = text.length >0;
        
        return text.length > 0;
        
    }
    return YES;
}

- (void) showInfoStrInLabelIsEmpty:(BOOL)isEmpty text:(NSString *)text{
    _infoLab.hidden = NO;
    if (isEmpty) {
        _infoLab.text = text;
//        [NSString stringWithFormat:@"%@不能为空", _nameLab.text];
    }else {
        _infoLab.text = NSLocalizedString(@"", nil);
//        @"格式有误";
    }
}

#pragma mark - getter
- (UILabel *)starLab {
    if (!_starLab) {
        self.starLab = [UILabel new];
        _starLab.textAlignment = NSTextAlignmentRight;
        _starLab.font = kFontSize15;
        _starLab.textColor = [UIColor orangeColor];
        _starLab.text = @"*";
        _starLab.hidden = YES;
    }
    return _starLab;
}

- (UILabel *)nameLab {
    if (!_nameLab) {
        self.nameLab = [UILabel new];
        _nameLab.textAlignment = NSTextAlignmentRight;
        _nameLab.font = kGeneralFontSize;
        _nameLab.textColor = [UIColor colorWithHex:@"3c3c3c"];
    }
    return _nameLab;
}

- (UITextField *)textField {
    if (!_textField) {
        self.textField = [UITextField new];
        _textField.textAlignment = NSTextAlignmentLeft;
        _textField.font = kGeneralFontSize;
        _textField.textColor = [UIColor colorWithHex:@"3c3c3c"];
    }
    return _textField;
}

- (UIView *)textFieldBgView {
    if (!_textFieldBgView) {
        self.textFieldBgView = [UIView new];
        _textFieldBgView.backgroundColor = kRandomColor;
        _textFieldBgView.layer.borderColor = RGBA(231, 233, 234, 1).CGColor;
        _textFieldBgView.layer.borderWidth = 1;
        _textFieldBgView.layer.masksToBounds = YES;
        _textFieldBgView.layer.cornerRadius = 10;
        _textFieldBgView.hidden = YES;
    }
    return _textFieldBgView;
}

- (UILabel *)infoLab {
    if (!_infoLab) {
        self.infoLab = [UILabel new];
        _infoLab.textAlignment = NSTextAlignmentLeft;
        _infoLab.font = kGeneralFontSize;
        _infoLab.textColor = [UIColor redColor];
//        _infoLab.text = [NSString stringWithFormat:@"%@不能为空", _nameLab.text];
        _infoLab.hidden = YES;
    }
    return _infoLab;
}


- (LineView *)line1 {
    if (!_line1) {
        self.line1 = [LineView new];
    }
    return _line1;
}
@end
